﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;

public partial class Basic_inbox : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["tid"] == null)
            Response.Redirect("../Error/Error.htm");
        if (!IsPostBack)
        {
            BindGridView(true);
            getUnread(Session["tid"].ToString());
        }
    }

    protected void getUnread(string teaid)
    {
        PM.PMBasic.Mail mailoperate=new PM.PMBasic.Mail();
        int unread = mailoperate.GetUnread(teaid);
        this.labunread.Text = unread.ToString();
    }
    protected void CheckAll(object sender, EventArgs e)
    {
        CheckBox cbx = (CheckBox)sender;
        foreach (GridViewRow gvr in GridView1.Rows)
        {
            CheckBox ch = (CheckBox)gvr.FindControl("ItemBox");
            ch.Checked = cbx.Checked;
        }
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindGridView(false);
    }
    protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGridView(false);
    }
    /// <summary>
    /// 绑定gridview
    /// </summary>
    /// <param name="GetCount">是否获取总数</param>
    protected void BindGridView(bool GetCount)
    {
        if (Session["tid"] == null)
            return;
        string tid = Session["tid"].ToString().Trim();
        string field = "id,name1,title,createdate,[type],isread ";
        StringBuilder where = new StringBuilder("  instatus=0");
        where.Append(" and receiver='").Append(tid).Append("'");
        if (this.ddlStatus.SelectedValue == "0")
            where.Append(" and isread=0");
        else if (this.ddlStatus.SelectedValue == "1")
            where.Append(" and isread=1");
        PM.Code.Web.controlbind.BindGridViewPublicProc(GridView1, AspNetPager1, "View_Messages", "id", field, "createdate desc", where.ToString(), GetCount);
    }
    /// <summary>
    /// 处理发送者姓名
    /// </summary>
    /// <param name="tanme">发送者姓名</param>
    /// <param name="mtype">消息类型</param>
    /// <returns>返回发送者</returns>
    static public string strSender(string tname,string mtype)
    {
        string tmpStr="";
        if (mtype == "2")
        {
            tmpStr = "学院管理员";
            return tmpStr;
        }
        else if (mtype == "3")
        {
            tmpStr = "项目管理员";
            return tmpStr;
        }
        else if (mtype == "4")
        {
            tmpStr = "系统管理员";
            return tmpStr;
        }
        else
            return tname;
    }
    /// <summary>
    /// 处理标题未读状态
    /// </summary>
    /// <param name="id">编号</param>
    /// <param name="isread">是否已读</param>
    /// <param name="title">标题</param>
    /// <returns>返回标题内容</returns>
    static public string strTitle(string id, string isread,string title)
    {
        string tmpStr = "";
        if (isread == "0")
        {
            tmpStr = "<a target='mainwin' href='readmsg.aspx?code=" + id + "&type=receive'><font color='red'>"+ title +"</font></a>";
            return tmpStr;
        }
        else if (isread=="1")
        {
            tmpStr = "<a target='mainwin' href='readmsg.aspx?code=" + id + "&type=receive'><font color='#009ad6'>" + title + "</font></a>";
            return tmpStr;
        }
        else
            return "无任何标题信息";
    }
    //批量删除
    protected void Button1_Click(object sender, EventArgs e)
    {
        Hashtable sqlstrs = new Hashtable();
        int keyid = -1;
        int i=1;
        foreach (GridViewRow gvr in GridView1.Rows)
        {
            CheckBox check = (CheckBox)gvr.FindControl("ItemBox");
            if (check.Checked)
            {
                keyid = Int32.Parse(GridView1.DataKeys[gvr.DataItemIndex].Value.ToString().Trim());
                string param="@mailid"+i.ToString();
                string sqlstr = "update Basic_UserMail set instatus=1 where id=" + param;
                SqlParameter[] MailParms = new SqlParameter[1]; 
                MailParms[0] = new SqlParameter(param, keyid);
                sqlstrs.Add(sqlstr,MailParms);
                i++;
            }
        }
        if (i > 1)
        {
            PM.Code.DAL.DbHelper.ExecuteSqlTran(sqlstrs);
            BindGridView(true);
        }
    }
    //批量标记已读
    protected void Button2_Click(object sender, EventArgs e)
    {
        Hashtable sqlstrs = new Hashtable();
        int keyid = -1;
        int i = 1;
        foreach (GridViewRow gvr in GridView1.Rows)
        {
            CheckBox check = (CheckBox)gvr.FindControl("ItemBox");
            if (check.Checked)
            {
                keyid = Int32.Parse(GridView1.DataKeys[gvr.DataItemIndex].Value.ToString().Trim());
                string param = "@mailid" + i.ToString();
                string sqlstr = "update Basic_UserMail set isread=1 where id=" + param;
                SqlParameter[] MailParms = new SqlParameter[1];
                MailParms[0] = new SqlParameter(param, keyid);
                sqlstrs.Add(sqlstr, MailParms);
                i++;
            }
        }
        if (i > 1)
        {
            PM.Code.DAL.DbHelper.ExecuteSqlTran(sqlstrs);
            BindGridView(true);
        }
    }
}
